﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using ALinq.BBS.Models;
using ALinq.BBS.App_GlobalResources;
using System.Web.Mvc;

namespace ALinq.BBS.Controllers
{
    partial class HomeController
    {
        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = Membership.GetUser(model.UserName);
                if (user != null)
                {
                    if (user.IsApproved)
                    {
                        if (Membership.ValidateUser(model.UserName, model.Password))
                        {
                            FormsAuthentication.SetAuthCookie(model.UserName, true);
                            if (!string.IsNullOrEmpty(returnUrl))
                            {
                                return Redirect(returnUrl);
                            }
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("AccountNotActived", Messages.AccountNotActived);
                    }
                }

                ModelState.AddModelError("PasswordIncorrect", Messages.PasswordIncorrect);
            }
            return View(model);
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index");
        }
    }
}